package immutable;
/**
 * <p>Title: Lab4p1 class - A main class that uses a mutable and an immutable class.</p>
 * <p>Description: CS 235 Lab #4</p>
 * @author A Varghese
 * @email  anthony.varghese@uwrf.edu
 * @date   February 1st 2013
 * @team   none
 * 
 */

public class Lab4p1 {

	/**
	 * main method
	 * @param args
	 */
	public static void main(String[] args) {
		A a = new A( 10 ); // A is a mutable class
		System.out.println(" a is " + a );
		a.set( 11 );
		System.out.println(" After calling the set method");
		System.out.println("  a is " + a );

		B b = new B( 10 ); // B is an immutable class
		System.out.println(" b is " + b );
		B c = b.set( 11 ); // set does not change b, it returns a new B object instead.
		System.out.println(" After calling the set method");
		System.out.println("  b is " + b );
		System.out.println(" c is " + c );
	}
}


/**
 * A - The class, A, is a mutable class - the data member dat can be modified
 *     by using the set method
 *
 */
class A{
	private int dat;
	public A(){
		dat = 0;
	}
	public A(int d){
		dat = d;
	}
	/**
	 * set returns changes the dat data member.
	 * @param d
	 * @return
	 */
	public void set(int d){
		dat = d;
	}
	public String toString(){
		return "[ " + dat + " ]";
	}
}

/**
 * B - The class, B, is an immutable class - the data member dat cannot be modified
 *     by using the set method - instead a new B object is created by set
 *
 */
class B{
	private int dat;
	public B(){
		dat = 0;
	}
	public B(int d){
		dat = d;
	}
	/**
	 * set returns a new B object rather than changing the existing one.
	 * @param d
	 * @return
	 */
	public B set(int d){
		return new B( d );
	}
	public String toString(){
		return "[ " + dat + " ]";
	}
}